package cn.hs.blog.user.action;

import java.io.IOException;
import java.util.Date;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hs.blog.base.ActionUtils;
import cn.hs.blog.base.PMF;
import cn.hs.blog.user.module.User;

public class RegisterUserAction {

    /**
     * 
     */
    private static final long serialVersionUID = 6203114989540869808L;

    private static final Logger log = Logger.getLogger(RegisterUserAction.class.getName());

    public void doRegister(HttpServletRequest req, HttpServletResponse resp)
        throws IOException,
            ServletException {

        System.out.println("RegisterAction.doRegister");
        User user = new User();

        ActionUtils.populate(req, user);
        // user.setName(req.getParameter("name"));
        // user.setPassword(req.getParameter("password"));
        // user.setEmail(req.getParameter("email"));
        user.setCreateDate(new Date());
        user.setBeable("1");

        log.info(user.toString());

        PersistenceManager pm = PMF.get().getPersistenceManager();
        try {
            pm.makePersistent(user);
        } finally {
            pm.close();
        }

        log.info("user register is success!");
        System.out.println("user register is success!");
        log.info(user.toString());

        req.getRequestDispatcher("/index").forward(req, resp);

        // resp.sendRedirect("/index");

    }

    public void execute(HttpServletRequest req, HttpServletResponse resp)
        throws IOException, ServletException {
        toRegister(req, resp);
    }

    public void toRegister(HttpServletRequest req, HttpServletResponse resp)
        throws IOException, ServletException {
        System.out.println("RegisterUserAction.toRegister");

        req.getRequestDispatcher("/jsp/registerUser.jsp").forward(req, resp);

    }

}
